if(!yeahtrace)
{
    var yeahtrace = new Object();
}

yeahtrace.makeUUID = function () {
    var S4 = function () {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };

    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1)
        { 
            c_start=c_start + c_name.length+1 
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end))
        } 
    }
    return ""
}

function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    document.cookie = name + "=; expires=" + exp.toGMTString();
}

yeahtrace.readYeahSession = function () {
    return getCookie('yeahtrace_session');
};

yeahtrace.clearYeahSession = function () {
    return delCookie('yeahtrace_session');
};
function ytrace(session, uuid, href, ref, data)
{
    yeahtrace.clearYeahSession();
    this.pageInfo = new Object();
    // session
    this.pageInfo.session = session || yeahtrace.readYeahSession() || yeahtrace.makeUUID();
    setCookie('yeahtrace_session', this.pageInfo.session, 30);
    // output
    this.output = function (href, ref) {
        // Current Url Location
        this.pageInfo.cur_loc = href || window.location.href;
        // Referer Url
        this.pageInfo.ref_url = ref || document.referrer;
        // Data
        this.pageInfo.data = data || 'etc';
        // uuid
        this.pageInfo.uuid = uuid || yeahtrace.makeUUID();
        return escape(this.pageInfo.toJSONString());
    };

}

/*
// yeahtrace.pageInfo.toJSONString()
$(document).ready(function () {
    $.ajax({type:"POST", url:"http://localhost/yeahtrace/trace.php", data:"trace="+escape(yeahtrace.pageInfo.toJSONString())});
});
*/
